package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FloatingPopulationAdtionalAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;


/**
 * <Description> <br>
 *
 * @author liaojing<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年11月25日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(FloatingPopulationAdtionalAttrHandler.class)
public class FloatingPopulationAdtionalLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();

        //公允价属性处理 (获取公允价属性)
        KiteAttr fairPriceAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004115");

        //物资编码属性处理
        KiteAttr materialIdAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004178");

        //获取场景ID
        String sceneInstId = data.getSceneInstId();

        //主控板附加商品
        //型号对应的公允价
        if(KiteStringUtils.isEqual(inst.getSpecId(),"63249555")){
            if(Objects.nonNull(fairPriceAttr)){
                //雁飞流动人口房屋管理产品-A2（含集中运维100个起订）-844元
                setHandleAttrValue ("110010243", attrValue, inst, "200004115", "844", sceneInstId);
                //雁飞流动人口房屋管理产品-A2（含单个运维20个起订）-944元
                setHandleAttrValue ("110010244", attrValue, inst, "200004115", "944", sceneInstId);
                //雁飞流动人口房屋管理产品-B180（含集中运维100个起订）-1145元
                setHandleAttrValue ("110010245", attrValue, inst, "200004115", "1145", sceneInstId);
                //雁飞流动人口房屋管理产品-B180（含单个运维20个起订）-1245元
                setHandleAttrValue ("110010246", attrValue, inst, "200004115", "1245", sceneInstId);
            }
            //型号对应的物资编码
            if(Objects.nonNull(materialIdAttr)){
                //雁飞流动人口房屋管理产品A2-物资编码7600301318-1
                setHandleAttrValue("110010243", attrValue, inst, "200004178", "7600301318", sceneInstId);
                setHandleAttrValue("110010244", attrValue, inst, "200004178", "7600301318", sceneInstId);
                //雁飞流动人口房屋管理产品B180-物资编码7600301319-1
                setHandleAttrValue("110010245", attrValue, inst, "200004178", "7600301319", sceneInstId);
                setHandleAttrValue("110010246", attrValue, inst, "200004178", "7600301319", sceneInstId);
            }
        }
        //配件及其它附加商品
        //产品公允价
        if(KiteStringUtils.isEqual(inst.getSpecId(),"63249366")) {
            if (Objects.nonNull(fairPriceAttr)) {
                //雁飞流动人口房屋管理产品-A2
                setHandleAttrValue("110010243", attrValue, inst, "200004115", "105", sceneInstId);
                setHandleAttrValue("110010244", attrValue, inst, "200004115", "105", sceneInstId);
                //雁飞流动人口房屋管理产品-B180外壳
                setHandleAttrValue("110010245", attrValue, inst, "200004115", "105", sceneInstId);
                setHandleAttrValue("110010246", attrValue, inst, "200004115", "105", sceneInstId);
            }
            //物资编码联动
            if (Objects.nonNull(materialIdAttr)) {
                //雁飞流动人口房屋管理产品A2-物资编码7600301318-1
                setHandleAttrValue("110010243", attrValue, inst, "200004178", "7600301318-1", sceneInstId);
                setHandleAttrValue("110010244", attrValue, inst, "200004178", "7600301318-1", sceneInstId);
                //雁飞流动人口房屋管理产品B180-物资编码7600301319-1
                setHandleAttrValue("110010245", attrValue, inst, "200004178", "7600301319-1", sceneInstId);
                setHandleAttrValue("110010246", attrValue, inst, "200004178", "7600301319-1", sceneInstId);
            }
        }
        if(KiteStringUtils.isEqual(inst.getSpecId(),"63249367")){
            //型号对应的物资编码
            if(Objects.nonNull(materialIdAttr)){
                //雁飞流动人口房屋管理产品A2-物资编码7600301318-1
                setHandleAttrValue("110010243", attrValue, inst, "200004178", "7600301318", sceneInstId);
                setHandleAttrValue("110010244", attrValue, inst, "200004178", "7600301318", sceneInstId);
                //雁飞流动人口房屋管理产品B180-物资编码7600301319-1
                setHandleAttrValue("110010245", attrValue, inst, "200004178", "7600301319", sceneInstId);
                setHandleAttrValue("110010246", attrValue, inst, "200004178", "7600301319", sceneInstId);
            }
        }
    }


    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
        if (KiteStringUtils.isEqual(key, oldValue)) {
            operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
        }
    }

}
